package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommonService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.ServiceService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.PostComplainParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.PostRepairParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNewResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNoticeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityStatus;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ComplainResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMAppointmentOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMOrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.GMOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.PropertyFeeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.RepairsResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceGroupResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class ServiceModel extends BaseModel implements ServiceContract.Model {
    @Inject
    public ServiceModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<CommunityStatus> whetherOwner() {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .whetherOwner()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<List<ServiceGroupResult>> fetchServiceGroup() {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchService()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ServiceResult> getServiceResult(int serviceId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .getServiceById(serviceId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<List<ServiceResult.ServiceInfo>> fetchServiceParams(int serviceId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .getServiceInfo(serviceId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> postComplain(PostComplainParams params) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .postComplain(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<ListResp<ComplainResult>> fetchComplainList(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchComplainList(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> postRepair(PostRepairParams params) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .postRepair(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> confirmRepairFinish(int repairId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .postConfirmRepairFinish(repairId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<ListResp<RepairsResult>> fetchRepairList(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchRepairList(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> postRepairEvaluate(int id, String evaluateContent, String images) {
        Map<String, Object> params = new HashMap<>();
        params.put("finishImg", images);
        params.put("finishReply", evaluateContent);
        params.put("id", id);
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .postRepairEvaluate(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<String> getQiNiuToken() {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .getQiNiuToken()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<List<PropertyFeeResult>> fetchPropertyFee(int type) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchFeeBill(type)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Map<String, Object>> payfeeProperty(int payType, int refId, int type) {
        Map<String, Object> params = new HashMap<>();
        params.put("payType", payType);
        params.put("referenceId", refId);
        params.put("type", type);
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .payfeeProperty(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommunityNoticeResult>> fetchCommunityNotice(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchCommunityNotice(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<CommunityNoticeResult> getCommunityNotice(int id) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .getCommunityNoticeById(id)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<GMOrderResult>> fetchGMOrder(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchGMWorkingOrder(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<GMAppointmentOrderResult>> fetchGMAppointmentOrder(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchGMAppointmentOrder(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<GMOrderDetailResult> getGMOrderDetail(int orderId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .getGMOrderDetail(orderId)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<CommunityNewResult>> fetchCommunityNewData(int communityId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchCommunityNewList(communityId)
                .compose(RxUtils.handleResult());
    }
}
